from airtest.core.api import *

from base.base_page import BaseObject, BaseOperate
from config import BaseDir


class ObjectPage(BaseObject):
    """
    登录页面对象层
    """

    def __init__(self):
        # 调用父类启动app
        super().__init__()
        self.district_service = r"2 服"
        self.login_btn = "Login"
        self.enter_game_btn = "进入游戏"
        self.username_input = "InputField"
        self.skip_btn = BaseDir + r"\icon\img_2.png"
        # 西部
        self.west_btn = "A2", "Flag"
        # 东部
        self.east_btn = "A1", "Flag"
        # 南部
        self.south_btn = "A3", "Flag"
        # 开始游戏
        self.start_game_btn = r"Btn_80_192"

    # 开始游戏按钮
    def find_start_game_btn(self):
        return self.get_element_by_parent(self.start_game_btn)

    # 西部按钮
    def find_west_btn(self):
        return self.get_element_parent_offspring(self.west_btn)

    # 东部按钮
    def find_east_btn(self):
        return self.get_element_parent_offspring(self.east_btn)

    # 南部按钮
    def find_south_btn(self):
        return self.get_element_parent_offspring(self.south_btn)

    # 定位新手跳过按钮
    def find_skip_btn(self):
        return self.skip_btn

    # 定位服务器位置
    def find_district_service(self):
        return self.get_element_by_text(self.district_service)

    # 定位登录按钮
    def find_login_btn(self):
        return self.get_element_by_parent(self.login_btn)

    # 定位进入游戏按钮
    def find_enter_game_btn(self):
        return self.get_element_by_text(self.enter_game_btn)

    # 定位用户名输入框
    def find_username_input(self):
        return self.get_element_by_parent(self.username_input)


class OperatePage(BaseOperate):
    """
    操作层
    """

    def __init__(self):
        # 页面层对象
        self.obp = ObjectPage()

    # 开始游戏按钮
    def click_start_game_btn(self):
        self.obp.find_start_game_btn().click()

    def touch_skip_new_play(self):
        self.touch_goods(self.obp.find_skip_btn())

    def select_district_service(self):
        self.obp.find_district_service().click()

    def click_login_btn(self):
        self.obp.find_login_btn().click()

    def click_enter_game_btn(self):
        self.obp.find_enter_game_btn().click()

    def input_username(self, username):
        self.obp.find_username_input().set_text(username)

    # 西部按钮
    def select_west_btn(self):
        self.obp.find_west_btn().click()

    # 东部按钮
    def select_east_btn(self):
        self.obp.find_east_btn().click()

    # 南部按钮
    def select_south_btn(self):
        self.obp.find_south_btn()


class LoginBusiness:
    """
    业务层
    """

    def __init__(self):
        # 页面层对象
        self.opp = OperatePage()

    # 已存在玩家登录
    def login(self, username):
        # 输入用户名
        self.opp.input_username(username)
        # 选择2服
        self.opp.select_district_service()
        # 点击登录
        self.opp.click_login_btn()
        # 点击进入游戏按钮
        self.opp.click_enter_game_btn()

    # 新角色登录
    def new_play_login(self, username):
        # 输入用户名
        self.opp.input_username(username)
        # 选择2服
        self.opp.select_district_service()
        # 点击登录
        self.opp.click_login_btn()
        # 点击进入游戏按钮
        self.opp.click_enter_game_btn()
        # 点击跳过
        self.opp.touch_skip_new_play()
        # 选择势力
        self.opp.select_east_btn()
        # 开始游戏
        self.opp.click_start_game_btn()


if __name__ == '__main__':
    print(BaseDir + "/icon/img.png")
